package com.multysite.controller.admin;

import java.io.IOException;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.NamespaceManager;
import com.google.appengine.api.users.UserServiceFactory;
import com.multysite.entity.admin.Application;
import com.multysite.model.admin.ApplicationModel;
import com.multysite.util.Setting;

@SuppressWarnings("serial")
public class ApplicationAdminServlet extends HttpServlet {

	private static final Logger log = Logger
			.getLogger(ApplicationAdminServlet.class.getName());

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		try {
			String currentNs = NamespaceManager.get();
			if (currentNs.equals(Setting.getGeneralNamespace())
					&& UserServiceFactory.getUserService().isUserAdmin()) {
				resp.getWriter().println("Hello My Boss");
				/*
				 * Me place
				 */
			} else {
				Application obj = ApplicationModel.getById(currentNs);
				if (obj != null
						&& obj.getUserEmail().equals(
								UserServiceFactory.getUserService()
										.getCurrentUser().getEmail())) {
					NamespaceManager.set(currentNs);
					req.getRequestDispatcher(
							"/admin/pages/application-detail.jsp").forward(req,
							resp);
				} else {
					req.getRequestDispatcher("/admin/pages/permission.jsp")
							.forward(req, resp);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.warning(e.toString());
		}
	}
}
